﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraLayout.Utils;

namespace PRO_DOCS.DocumentManagementSystem.Petrolium.WIN.Back
{
    public partial class Schedule : BLL.WIN.Templates.DevxPermissionTemplate
    {
        public bool IsNow;

        private int _taskID;
        public Schedule(int taskID)
        {
            InitializeComponent();
            _taskID = taskID;
        }

        private void repeatCheckEdit_CheckedChanged(object sender, EventArgs e)
        {
            if (repeatCheckEdit.Checked)
                dayesCheckedListBoxControl.Enabled = true;
            else
            {
                dayesCheckedListBoxControl.Enabled = false;

                for (var i = 0; i < 7; i++)
                    dayesCheckedListBoxControl.SetItemChecked(i, false);
            }
        }

        private void onceCheckEdit_CheckedChanged(object sender, EventArgs e)
        {
            onceLayoutControlItem.Visibility = onceCheckEdit.Checked ? LayoutVisibility.Always : LayoutVisibility.Never;
        }

        private void dailyCheckEdit_CheckedChanged(object sender, EventArgs e)
        {
            dailyLayoutControlItem.Visibility = dailyCheckEdit.Checked ? LayoutVisibility.Always : LayoutVisibility.Never;
        }

        private void weekCheckEdit_CheckedChanged(object sender, EventArgs e)
        {
            if (weekCheckEdit.Checked)
            {
                weekLayoutControlItem.Visibility = LayoutVisibility.Always;
                repeatLayoutControlItem.Visibility = LayoutVisibility.Always;
                dayesLayoutControlItem.Visibility = LayoutVisibility.Always;
                this.Height += 152;
            }
            else
            {
                repeatLayoutControlItem.Visibility = LayoutVisibility.Never;
                dayesLayoutControlItem.Visibility = LayoutVisibility.Never;
                weekLayoutControlItem.Visibility = LayoutVisibility.Never;
                repeatCheckEdit.CheckState = CheckState.Unchecked;
                dailyDateEdit.ResetText();
                this.Height -= 152;
            }
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            var type = string.Empty;
            var startDat = new DateTime();

            if (onceCheckEdit.Checked)
            {
                type = "Once";
                startDat = onceDateEdit.DateTime;
            }
            else if (dailyCheckEdit.Checked)
            {
                type = "Daily";
                startDat = dailyDateEdit.DateTime;
            }
            else if (weekCheckEdit.Checked)
            {
                type = "Weekly";
                startDat = weeklyDateEdit.DateTime;
            }

            if (operationNameTextEdit.Text == string.Empty)
            {
                XtraMessageBox.Show("Please enter the Task Name");
                return;
            }
            else
            {
                if (_taskID == -1 || _taskID == 0)
                {
                    if (PRO_DOCSDatabaseDataContext.BackUp_Tasks.Any(m => m.TaskName.ToLower() == operationNameTextEdit.Text))
                    {
                        XtraMessageBox.Show("Please the task name already in use");
                        return;
                    }
                }
                else
                {
                    if (PRO_DOCSDatabaseDataContext.BackUp_Tasks.Any(m => m.TaskName.ToLower() == operationNameTextEdit.Text && m.TaskID != _taskID))
                    {
                        XtraMessageBox.Show("Please the task name already in use");
                        return;
                    }
                }
            }

            if (type == string.Empty && !nowCheckEdit.Checked)
            {
                XtraMessageBox.Show("Please select the type");
                return;
            }

            if (startDat == new DateTime() && !nowCheckEdit.Checked)
            {
                XtraMessageBox.Show("Please select the start DateTime");
                return;
            }
            else
            {
                startDat = DateTime.Now;
            }

            if (repeatCheckEdit.Checked)
            {
                if (dayesCheckedListBoxControl.CheckedItems.Count == 0)
                {
                    XtraMessageBox.Show("Please select the repeat dayes");
                    return;
                }
            }

            var res = -1;
            if (_taskID != -1 && _taskID != 0)
            {
                res = PRO_DOCSDatabaseDataContext.proc_BackUp_TasksUpdate(_taskID, operationNameTextEdit.Text, type,
                                                                         startDat, repeatCheckEdit.Checked,
                                                                         string.Join(";",
                                                                                     (from ListBoxItem it in
                                                                                          dayesCheckedListBoxControl.
                                                                                          CheckedItems
                                                                                      select it.ToString()).ToList()), 1,
                                                                         true, "Updated", 2, storageComboBoxEdit.Text.ToLower());
            }
            else if (!nowCheckEdit.Checked)
            {
                res = PRO_DOCSDatabaseDataContext.proc_BackUp_TasksInsert(operationNameTextEdit.Text, type,
                                                                         startDat,
                                                                         repeatCheckEdit.Checked,
                                                                         string.Join(";",
                                                                                     (from ListBoxItem it in
                                                                                          dayesCheckedListBoxControl.
                                                                                          CheckedItems
                                                                                      select it.ToString()).ToList()),
                                                                         1,
                                                                         DateTime.Now,
                                                                         true, storageComboBoxEdit.Text.ToLower());
            }

            if (nowCheckEdit.Checked)
            {
                IsNow = true;
                res = 0;
            }


            if (res == 0)
            {
                XtraMessageBox.Show("Saved Successfuly");
                DialogResult = DialogResult.OK;
            }
            else
                XtraMessageBox.Show("Faild to save");
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void Schedule_Load(object sender, EventArgs e)
        {
            if (_taskID != -1 && _taskID != 0)
            {
                var task = PRO_DOCSDatabaseDataContext.proc_BackUp_TasksLoad(_taskID).FirstOrDefault();
                if (task != null)
                {
                    operationNameTextEdit.Text = task.TaskName;
                    if (task.TaskType == "Once")
                    {
                        onceCheckEdit.CheckState = CheckState.Checked;
                        if (task.TaskStartAt != null)
                            onceDateEdit.DateTime = task.TaskStartAt.Value;
                    }
                    else if (task.TaskType == "Daily")
                    {
                        dailyCheckEdit.CheckState = CheckState.Checked;
                        if (task.TaskStartAt != null)
                            dailyDateEdit.DateTime = task.TaskStartAt.Value;
                    }
                    else if (task.TaskType == "Weekly")
                    {
                        weekCheckEdit.CheckState = CheckState.Checked;
                        if (task.TaskStartAt != null)
                            weeklyDateEdit.DateTime = task.TaskStartAt.Value;
                        repeatCheckEdit.CheckState = task.Repeat != null && task.Repeat.Value ? CheckState.Checked : CheckState.Unchecked;
                        if (task.RepeatDayes.Length > 0)
                        {
                            var res = task.RepeatDayes.Split(';');
                            foreach (var re in res)
                                dayesCheckedListBoxControl.Items[re].CheckState = CheckState.Checked;
                        }
                    }
                }
            }
        }
    }
}